﻿--HEADER Rename Clustered index on [Reports].[LogEntry] table
--SKIPTRANSACTION
DECLARE @getNameSql NVARCHAR(MAX), @renameSql NVARCHAR(MAX), @oldName NVARCHAR(MAX), @newName NVARCHAR(15);
SET @newName = 'PK_LogEntry'

SELECT @getNameSql = 'SELECT @oldName = name
FROM   sys.key_constraints
WHERE  [type] = ''PK''
       AND [parent_object_id] = Object_id(''[Reports].[LogEntry]'');'

EXEC sp_executesql @getNameSql, N'@oldName NVARCHAR(MAX) OUTPUT', @oldName OUT



SET @renameSql = 'sp_rename ' + '''Reports.' + @oldName + ''', ''' + @newName + ''''
EXEC (@renameSql)